<?php
require_once("yammerView.php");
require_once(CORE_DIR.'yammerModel.php');

class addVerifierAction extends authAction{

	function __construct(){
		$this->view = new yammerView();
	}

	public function run($get, $post, $viewtype) {
		parent::run($get,$post,$viewtype);
		
		$yModel = new yammerModel();	
		
		$status = 0;
		$errMessage = "";
		$yammerRequestToken = "";
		if(isset($get["userid"]) && isset($get["verifier"])) {
			$userid = $get["userid"];
			$verifier = $get["verifier"];
			
			$yammerDto = $yModel->getYammerData($userid);

			$tokens = $yModel->getAccessTokens($yammerDto->getOAuthToken(), $yammerDto->getOAuthTokenSecret(), $verifier);
			
			if($tokens["oauth_token"] != false)
			{
				$yModel->addYammerData($userid, $tokens["oauth_token"], $tokens["oauth_token_secret"], $verifier );
			}
				
			$status = $yModel->isYammerActive($userid);
			if(!$status)
			{
				$tokens = $yModel->getRequestTokens();
				
				$oauth_token = $tokens["oauth_token"];
				$oauth_token_secret = $tokens["oauth_token_secret"];
				
				$yModel->addYammerData($userid, $oauth_token, $oauth_token_secret);
				$errMessage = "Something was wrong please authorize again.";
				
				$yammerRequestToken = $oauth_token;
			}
		}
		else 
		{
			$errMessage = "Please set userid and verifier";
		}
		$this->view->setProperty("status", $status);
		$this->view->setProperty("errMessage", $errMessage);
		$this->view->setProperty("yammerRequestToken", $yammerRequestToken);
		
		$this->view->showStatusYammer();
	}
}
?>